<template>
  <div class="goods-container">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input v-model="params.query" clearable @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="searchFn"></el-button>
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="goAddGoods">添加商品</el-button>
        </el-col>
      </el-row>
     
     <el-table
        :data="goodsList"
        border
        style="width: 100%">
        <el-table-column
          type="index"
          label="#"
          width="50">
        </el-table-column>
        <el-table-column
          prop="goods_name"
          label="商品名">
        </el-table-column>
        <el-table-column
          prop="goods_price"
          label="价格"
          width="80">
        </el-table-column>
        <el-table-column
          prop="goods_weight"
          label="重量"
          width="80">
        </el-table-column>
        <el-table-column
          label="创建时间"
          width="140">
          <template slot-scope="scope">
            {{scope.row.upd_time | filterTime}}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="160">
          <template slot-scope="scope">
            <el-button type="primary" size="mini">编辑</el-button>
            <el-button type="danger" size="mini" @click="deleteFn(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.pagenum"
        :page-sizes="[5, 10, 30, 50]"
        background
        :page-size="params.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      goodsList: [],
      total: 0,
      params: {
        query: '',
        pagenum: 1,
        pagesize: 10
      }
    }
  },
  filters: {
    filterTime(val) {
      const date = new Date(val)

      const yyyy = date.getFullYear()
      const m = (date.getMonth() + 1 + '').padStart(2, 0)
      const d = (date.getDate() + '').padStart(2, 0)

      let result = `${yyyy}-${m}-${d}`
      return result
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList() {
      const {data: res} = await this.$http.get('/goods', {
        params: this.params
      })

      if(res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }

      this.total = res.data.total
      this.goodsList = res.data.goods
    },
    handleSizeChange(val) {
      this.params.pagesize = val
      this.getGoodsList()
    },
    handleCurrentChange(val) {
      this.params.pagenum = val
      this.getGoodsList()
    },
    searchFn() {
      this.params.pagenum = 1
      this.getGoodsList()
    },
    deleteFn(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const {data: res} = await this.$http.delete(`goods/${row.goods_id}`)

          if(res.meta.status !== 200) {
            return this.$message.error(res.meta.msg)
          }

          this.getGoodsList()
          this.$message.success(res.meta.msg)
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })        
        })
    },
    goAddGoods() {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style lang="less" scoped>
.goods-container{
  .el-card,
  .el-table{
    margin: 20px 0;
  }
}
</style>
